package view;

import java.awt.Font;
import java.awt.Rectangle;

import java.util.Arrays;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import model.Template;

/**
 * Create TemplatePanel class
 *
 */
public class TemplatePanel extends JPanel
{
    private static final long serialVersionUID = -4151946011898170814L;
    private JLabel templateName;
	private JTextField templateNameTextField;
	private JTextField toOrFromTextField;
	private JTextField subjectTextField;
	private JTextArea messageTextField;
	private JLabel toOrFrom;

	/**
	 * Constructor Template Panel
	 */
	TemplatePanel()
	{
	    this.initialize();
	}
	
	/**
	 * Initialization of template panel
	 */
	private void initialize()
	{
	    this.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.setLayout(null);
        
        templateName = new JLabel("Name");
        templateName.setBounds(new Rectangle(32,44,64,30));
        templateName.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(templateName);
        
        toOrFrom = new JLabel("To");
        toOrFrom.setBounds(new Rectangle(32,104,64,30));
        toOrFrom.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(toOrFrom);
        
         JLabel subjectLable = new JLabel("Subject");
        subjectLable.setBounds(new Rectangle(32,164,64,30));
        subjectLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(subjectLable);
        
        JLabel contentLable = new JLabel("Message");
        contentLable.setBounds(new Rectangle(32,224,64,30));
        contentLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(contentLable);
        
        templateNameTextField = new JTextField();
        templateNameTextField.setBounds(new Rectangle(135,44,400,30));
        this.add(templateNameTextField);
        
        toOrFromTextField = new JTextField();
        toOrFromTextField.setBounds(new Rectangle(135,104,400,30));
        this.add(toOrFromTextField);
        
        subjectTextField = new JTextField();
        subjectTextField.setBounds(new Rectangle(135,164,400,30));
        this.add(subjectTextField);
        
        messageTextField = new JTextArea();
        
        JScrollPane scroll = new JScrollPane (messageTextField, 
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setBounds(new Rectangle(135,224,400,250));    
        this.add(scroll);
        
	}

    /**
     * Get template model
     * @return template the template reference
     */
    public Template getTemplate()
	{
        String[] receivers = toOrFromTextField.getText().trim().split(",");
        List<String> receiverList = Arrays.asList(receivers);
        String subject = subjectTextField.getText().trim();
        String message = messageTextField.getText().trim();
        String templateName = templateNameTextField.getText().trim();
        
        Template template = new Template(templateName, subject, message, receiverList);
            
        return template;
	 }
    
    /**
     * Load template model
     * @param tempL the template to load
     */
    public void loadTemplate(Template tempL)
    {
        List<String> recL = tempL.getTo();
        StringBuilder rec = new StringBuilder();
        for(int n=0;n<recL.size();n++)
        {
            rec.append(recL.get(0));
            if(n != (recL.size()-1))
            {
                rec.append(",");  
            }    
        }
        
        String subject = tempL.getSubject();
        String message = tempL.getMessage();
        toOrFromTextField.setText(rec.toString());
        subjectTextField.setText(subject);
        messageTextField.setText(message);
        templateNameTextField.setText(tempL.getTemplateName());
    }
}
